﻿using Microsoft.Owin.Security.OAuth;
using MovieAPI.lib;
using System.Configuration;
using System.Web.Http;

namespace MovieAPI
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务
            var allowOrigin = ConfigurationManager.AppSettings["cors:allowOrigin"];
            var allowHeaders = ConfigurationManager.AppSettings["cors:allowHeaders"];
            var allowMethods = ConfigurationManager.AppSettings["cors:allowMethods"];
            // Web API 路由
            //HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            //config.SuppressDefaultHostAuthentication();
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            config.MapHttpAttributeRoutes();
            config.EnableCors();
            config.Filters.Add(new Cores());
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
